package net.lab1024.sa.admin.module.workflow.controller;


import com.sun.org.apache.xpath.internal.operations.Bool;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.admin.module.workflow.domain.form.WfTaskBo;
import net.lab1024.sa.admin.module.workflow.service.WfInstanceService;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 工作流流程实例管理
 *
 * @author KonBAI
 * @createTime 2022/3/10 00:12
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/workflow/instance")
public class WfInstanceController {

    @Resource
    private final WfInstanceService instanceService;

    /**
     * 激活或挂起流程实例
     *
     * @param state      1:激活,2:挂起
     * @param instanceId 流程实例ID
     */
    @PostMapping(value = "/updateState")
    public ResponseDTO<Boolean> updateState(@RequestParam Integer state, @RequestParam String instanceId) {
        instanceService.updateState(state, instanceId);
        return ResponseDTO.ok(true);
    }

    /**
     * 结束流程实例
     *
     * @param bo 流程任务业务对象
     */
    @PostMapping(value = "/stopProcessInstance")
    public ResponseDTO<Boolean> stopProcessInstance(@RequestBody WfTaskBo bo) {
        instanceService.stopProcessInstance(bo);
        return ResponseDTO.ok(true);
    }

    /**
     * 删除流程实例
     *
     * @param instanceId   流程实例ID
     * @param deleteReason 删除原因
     */
    @Deprecated
    @DeleteMapping(value = "/delete")
    public ResponseDTO<Boolean> delete(@RequestParam String instanceId, String deleteReason) {
        instanceService.delete(instanceId, deleteReason);
        return ResponseDTO.ok(true);
    }

    /**
     * 查询流程实例详情信息
     *
     * @param procInsId 流程实例ID
     * @param deployId  流程部署ID
     */
    @GetMapping("/detail")
    public ResponseDTO<Map<String, Object>> detail(String procInsId, String deployId) {
        Map<String, Object> map = instanceService.queryDetailProcess(procInsId, deployId);
        return ResponseDTO.ok(map);
    }
}
